package cn.kust.serviceusercenter.controller;


import cn.kust.microtourismcommon.constant.CommonStatusEnum;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.microtourismcommon.pojo.dto.UserIdDTO;
import cn.kust.microtourismcommon.pojo.request.VerificationCodeDTO;
import cn.kust.serviceusercenter.service.IUserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WY
 * @data 2022/7/5 20:38
 **/
@RestController
@RequestMapping("/userCenter")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    // 插入手机用户
    @PostMapping("/user")
    public ResponseResult login(@RequestBody VerificationCodeDTO verificationCodeDTO) {
        String userPhone = verificationCodeDTO.getUserPhone();
        log.debug("手机号：" + userPhone);
        return userService.loginOrRegister(userPhone);
    }

    // 根据手机号查询用户信息
    @GetMapping("/user/{phone}")
    public ResponseResult getUser(@PathVariable("phone") String userPhone) {

        return userService.getUserByPhone(userPhone);

    }

    // 根据用户id查询用户名和头像
    @RequestMapping(value = "/user/user-name-avatar/{userId}", method = RequestMethod.GET)
    public ResponseResult getUser(@PathVariable("userId") Long userId) {
        String userid = Long.toString(userId);
        return userService.getUserNameAvatar(userid);

    }

    // 根据id列表查询用户的用户名和头像
    @RequestMapping(value = "/user/list/name-avatar", method = RequestMethod.POST)
    public ResponseResult getUserNameAvatarList(@RequestBody UserIdDTO userIdDTO) {

        log.debug(userIdDTO.toString());
        List<Long> userIds = userIdDTO.getUserIdList();
        if (userIds == null || userIds.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.USER_ID_LIST_EMPTY.getCode(), CommonStatusEnum.USER_ID_LIST_EMPTY.getValue());
        }
        return userService.getUserNameAvatarListById(userIds);
    }

    //用户信息分页查询
    @RequestMapping(value = "/users/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public ResponseResult getUsersPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize")
            int pageSize) {
        if (pageNum != 0 || pageSize != 0){
            return userService.getUsersPage(pageNum, pageSize);
        }
        return null;
    }


}
